package com.chushouya.order.service.api.impl;

import com.chushouya.order.dto.api.order.OrderQualityApiDTO;
import com.chushouya.order.dto.api.order.QualityProductApiDTO;
import com.chushouya.order.dto.api.order.QualityUserApiDTO;
import com.chushouya.order.dto.api.quality.QualityItemApiDTO;
import com.chushouya.order.service.api.OrderQualityApiService;
import com.chushouya.order.service.api.ClerkOrderQualityApiService;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dao.repository.OrderProductRepository;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.entity.OrderProductEntity;
import com.chushouya.order.dto.admin.order.OrderQuery;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Beans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 订单质检API服务实现类
 */
@Service
@Slf4j
public class OrderQualityApiServiceImpl implements OrderQualityApiService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private OrderProductRepository orderProductRepository;

    @Resource
    private ClerkOrderQualityApiService clerkOrderQualityApiService;

    @Override
    public OrderQualityApiDTO getQualityDetail(String orderNo) {
        log.info("获取订单质检详情，订单号：{}", orderNo);
        
        // 根据订单号查询订单信息
        OrderEntity orderEntity = getOrderByOrderNo(orderNo);
        if (Objects.isNull(orderEntity)) {
            log.warn("订单不存在，订单号：{}", orderNo);
            return null;
        }

        // 构建质检详情DTO
        OrderQualityApiDTO qualityDetail = buildQualityDetail(orderEntity);
        
        log.info("成功获取订单质检详情，订单号：{}，订单ID：{}", orderNo, orderEntity.getOrderId());
        return qualityDetail;
    }

    /**
     * 根据订单号获取订单信息
     */
    private OrderEntity getOrderByOrderNo(String orderNo) {
        OrderQuery query = new OrderQuery();
        query.setOrderNo(orderNo);
        OrderEntity orderEntity = orderRepository.selectOne(query);
        Check.notNull(orderEntity, "订单不存在");
        return orderEntity;
    }

    /**
     * 构建质检详情信息
     */
    private OrderQualityApiDTO buildQualityDetail(OrderEntity orderEntity) {
        OrderQualityApiDTO qualityDetail = new OrderQualityApiDTO();
        
        // 基本订单信息
        qualityDetail.setOrderNo(orderEntity.getOrderNo());
        qualityDetail.setPartnerOrderNo(orderEntity.getPartnerOrderNo());
        qualityDetail.setOrderStatus(orderEntity.getOrderStatus());
        qualityDetail.setOrderType(orderEntity.getOrderType());
        qualityDetail.setUserPhone(orderEntity.getUserPhone());
        qualityDetail.setUserName(orderEntity.getUserName());
        qualityDetail.setCompletePrice(orderEntity.getCompletePrice());
        qualityDetail.setOrderPrice(orderEntity.getOrderPrice());
        qualityDetail.setQualityPrice(orderEntity.getQualityPrice());
        qualityDetail.setEvaluatePrice(orderEntity.getEvaluatePrice());
        qualityDetail.setQualityTime(orderEntity.getQualityTime());

        // 获取订单产品信息
        OrderProductEntity orderProduct = orderProductRepository.selectByOrderId(orderEntity.getOrderId());
        if (Objects.nonNull(orderProduct)) {
            qualityDetail.setProductName(orderProduct.getProductName());
            
            // 构建质检产品信息
            QualityProductApiDTO qualityProduct = buildQualityProduct(orderProduct);
            qualityDetail.setQualityProduct(qualityProduct);
        }

        // 获取质检选项列表
        List<QualityItemApiDTO> qualityItems = clerkOrderQualityApiService.orderQualityItem(orderEntity.getOrderId());
        qualityDetail.setOrderQualityItem(qualityItems);

        // 构建质检师信息
        QualityUserApiDTO qualityUser = buildQualityUser(orderEntity);
        qualityDetail.setQualityUser(qualityUser);

        return qualityDetail;
    }

    /**
     * 构建质检产品信息
     */
    private QualityProductApiDTO buildQualityProduct(OrderProductEntity orderProduct) {
        return Beans.copy(orderProduct, QualityProductApiDTO.class);
    }

    /**
     * 构建质检师信息
     */
    private QualityUserApiDTO buildQualityUser(OrderEntity orderEntity) {
        QualityUserApiDTO qualityUser = new QualityUserApiDTO();
        qualityUser.setQualityName("林识羽");
        qualityUser.setQualityTag("资深验机师");
        qualityUser.setAvatar("https://image.chushouya.com//upload/20250923/1758610100348_8wd9w8.png");
        qualityUser.setDescription("三年验机经验");
        return qualityUser;
    }

}
